package com.example.securityyuqq.config;

import com.example.securityyuqq.handler.YUQQSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler successHandler;


    @Autowired
    private AuthenticationFailureHandler failureHandler;

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().successHandler(successHandler).failureHandler(failureHandler);
        http.logout()
//配置注销成功处理器
                .logoutSuccessHandler(logoutSuccessHandler);
        //在 configure(HttpSecurity http) 方法中，使用 .successHandler(successHandler) 将自定义的成功处理器注册到 Spring Security 的表单登录流程中；
        //所以是的，必须将 successHandler 注入并配置进 http.formLogin() 才能生效并被使用。
        http.authorizeRequests()
                .anyRequest().authenticated();
    }
}
